'use strict';

import { merchantjoinModel } from "../model/merchantjoin";
import { ServiceBase, Exceptions } from '../kalend';
import { datadictionaryService } from "./datadictionary";
import { usertokenService } from "./usertoken";
var moment = require('moment');

class MerchantjoinService extends ServiceBase {
    constructor() {
        super(merchantjoinModel)
    }
    async query(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        const iscontact = params.iscontact;
        const appbegintime = params.appbegintime;
        const appendtime = params.appendtime;
        const filter: any = {};
        if (appbegintime) {
            filter.createTime = { '$gte': new Date(moment(new Date(appbegintime)).format('YYYY-MM-DD') + '  00:00:00') };
        }
        if (appendtime) {
            if (filter.createTime) {
                filter.createTime['$lte'] = new Date(moment(appendtime).format('YYYY-MM-DD') + '  23:59:59');
            } else {
                filter.createTime = { '$lte': new Date(moment(appendtime).format('YYYY-MM-DD') + '  23:59:59') };
            }
        }
        if (iscontact && iscontact != '') {
            filter.iscontact = iscontact;
        }
        let result: any = await this.queryPagination(offset, limit, {
            filter: filter,
            sort: { createtime: -1 }
        });
        result.rows = result.rows.map((item: any) => {
            if (item.iscontact == '0') {
                item.iscontact = '未联系';
            } else if (item.iscontact == '1') {
                item.iscontact = '已联系';
            }
            return item;
        });
        return result;
    }
    async save(doc: any) {
        if (!doc.token) {
            throw Exceptions.notoken();
        }
        const sesiondata = await usertokenService.getToken(doc.token);
        if (sesiondata) {
            doc.userid = sesiondata.userid;
            delete doc.token;
            const info = await datadictionaryService.querybyKey('merchantjoin');
            const isexists = await this.querySingle({ phone: doc.phone, merchantname: doc.merchantname });
            if (isexists) {
                this.update(isexists._id, isexists);
                return info.find((a: any) => { return a.title = '再次申请' }).text;
            } else {
                this.create(doc);
                return info.find((a: any) => { return a.title = '首次申请' }).text;
            }
        }
    }


    async updatestatus(doc: any) {
        return this.update(doc._id, { iscontact: 1, remarks: doc.remarks });
    }

    async queryById(doc: any) {
        return this.querySingle({ _id: doc._id });
    }

    async queryByCode(token: any) {
        if (!token) {
            throw Exceptions.notoken();
        }
        const sesiondata = await usertokenService.getToken(token);
        if (sesiondata) {
            return this.querySingle({ userid: sesiondata.userid });
        } else {
            throw Exceptions.known('未找到用户信息，查询失败');
        }
    }

}

export const merchantjoinService = new MerchantjoinService();